<template>
  <div id="header">
    <div class="logo">{{this.$store.state.config.site_name || 'itSpire'}}</div>
    <div id="search" :class="this.$store.state.searchShow">
      <el-autocomplete
        size="mini"
        suffix-icon="el-icon-search"
        class="inline-input"
        v-model="search"
        value-key="title"
        :fetch-suggestions="querySearch"
        placeholder="搜索文章"
        @select="handleSearchSelect"
        clearable>
      </el-autocomplete>
    </div>
    <div class="menu">
      <el-menu
        :default-active="this.activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        background-color="#333"
        menu-trigger="click"
        text-color="#fff"
        router
        active-text-color="#fff">
        <el-menu-item index="/index" class="menu-item">首页</el-menu-item>
        <el-menu-item index="/timeline" class="menu-item" disabled>动态</el-menu-item>
        <el-menu-item index="/about" class="menu-item">关于</el-menu-item>
        <el-submenu index="" class="menu-icon">
          <template slot="title"><i class="el-icon-more"></i></template>
          <el-menu-item-group>
            <template slot="title"><span style="margin-left: -30px">文章分类</span></template>
            <template v-for="(item, key) in this.category">
              <el-menu-item :index="'/category/'+item.id" :key="key">
                <span>{{item.categoryName}}</span>
              </el-menu-item>
            </template>
          </el-menu-item-group>
        </el-submenu>
      </el-menu>
    </div>
  </div>
</template>

<script>
  export default {
    name: "Header",
    props: {
      activeIndex: null,
      category: null,
      searches: null,
    },
    data() {
      return {
        search: '',
        timeout: null,
      }
    },
    methods: {
      handleSelect(index, indexPath) {
        this.$store.state.activeIndex = index
      },
      querySearch(queryString, cb) {
        let searches = this.searches;
        let results = queryString ? searches.filter(this.createFilter(queryString)) : searches;
        // 调用 callback 返回建议列表的数据
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          cb(results);
        }, 1000 * Math.random());
      },
      createFilter(queryString) {
        return (item) => {
          // return (item.title.toLowerCase().replace(" ","").indexOf(queryString.toLowerCase().replace(" ","")) === 0);
          return (item.title.toLowerCase().replace(" ", "").search(queryString.toLowerCase().replace(" ", "")) != -1)
        };
      },
      handleSearchSelect(item) {
        this.$router.push({name: 'article', params: {id: item.id}})
      }
    }
  }
</script>

<style scoped>

  .logo {
    float: left;
    font-size: 26px;
    font-weight: bold;
    font-family: serif;
  }

  .menu {
    float: right;
  }

  .menu-icon {
    display: none;
    padding: 0 !important;
    border-bottom: 0 !important;
  }

  .menu-icon i {
    margin: 0;
    line-height: 18px;
  }

  .menu-icon i:before {
    color: #fff;
  }

  .menu-mini {
    float: right;
    color: #fff;
    top: 50px;
    display: none;
  }

  #search {
    float: right;
    margin-left: 20px;
    z-index: 2002;
  }

  #header {
    position: relative;
  }

  #header ul {
    border: 0;
  }

  #header li {
    line-height: 49px;
    height: 49px;
    padding: 0 15px;
  }
</style>
